#define _CRT_SECURE_NO_WARNINGS 1
#include"Queue.h"

void QueueInit(Queue* p)
{
	assert(p);
	p->size = 0;
	p->head = NULL;
	p->end = NULL;
}

void QueueDestory(Queue* p)
{
	assert(p);
	do
	{
		QueueNode* cur = p->head->next;
		free(p->head);
		p->head = cur;
	} while (p->head != NULL);
	p->head = NULL;
	p->end = NULL;
}

QueueNode* QueueMemCre(QueueDataType x)
{
	QueueNode* tmp = (QueueNode*)malloc(sizeof(QueueNode));
	if (tmp == NULL)
	{
		perror("malloc fail");
		return NULL;
	}
	tmp->data = x;
	return tmp;
}

void QueuePush(Queue* p, QueueDataType x)
{
	assert(p);
	QueueNode* new = QueueMemCre(x);
	if (p->head == NULL)
	{
		p->head = new;
		p->end = new;
	}
	else
	{
		p->end->next = new;
		p->end = new;
	}
	new->next = NULL;
	p->size++;
}

void QueuePop(Queue* p)
{
	assert(p);
	assert(p->head != NULL);
	QueueNode* tmp = p->head->next;
	free(p->head);
	p->head = tmp;
	p->size--;
}

int QueueSize(Queue* p)
{
	assert(p);
	return p->size;
}

bool QueueEmpty(Queue* p)
{
	assert(p);
	return p->size == 0;
}

QueueDataType QueueFront(Queue* p)
{
	return p->head->data;
}

QueueDataType QueueBack(Queue* p)
{
	return p->end->data;
}